package com.hanhai.material.wages.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.wages.entity.DingAttendanceResult;
import com.hanhai.material.wages.service.DingAttendanceResultService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.hanhai.material.wages.entity.AttendanceResultReport;
import com.hanhai.material.wages.service.AttendanceResultReportService;
import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
import java.util.List;

import static com.hanhai.material.utils.TimeUtils.fromDateToString;
import static com.hanhai.material.wages.entity.table.AttendanceResultReportTableDef.ATTENDANCE_RESULT_REPORT;
import static com.hanhai.material.wages.entity.table.DingAttendanceResultTableDef.DING_ATTENDANCE_RESULT;

/**
 *  控制层。
 *
 * @author youchuang
 * @since 2023/10/25 17:34:51
 */
@RestController
@RequestMapping("/attendance_result_report")
@Log4j2
public class AttendanceResultReportController {

    @Autowired
    private AttendanceResultReportService attendanceResultReportService;
    @Autowired
    private DingAttendanceResultService dingAttendanceResultService;

    /**
     * 添加。
     *
     * @param attendanceResultReport 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody AttendanceResultReport attendanceResultReport) {
        return attendanceResultReportService.save(attendanceResultReport);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return attendanceResultReportService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param attendanceResultReport 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody AttendanceResultReport attendanceResultReport) {
        return attendanceResultReportService.updateById(attendanceResultReport);
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public AttendanceResultReport getInfo(@PathVariable Serializable id) {
        return attendanceResultReportService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<AttendanceResultReport> page(Page<AttendanceResultReport> page) {
        return attendanceResultReportService.page(page);
    }


    @PostMapping("/list/{page}/{size}/{totalRows}")
    public Result<?> list(@RequestBody AttendanceResultReport attendanceResultReport,@PathVariable long page,@PathVariable long size,@PathVariable long totalRows){
        Page<AttendanceResultReport> pageObject=new Page<AttendanceResultReport>();
        pageObject.setPageNumber(page);
        pageObject.setPageSize(size);
        pageObject.setTotalRow(size);
        QueryWrapper queryWrapper=QueryWrapper.create();

        queryWrapper.where(ATTENDANCE_RESULT_REPORT.ATTENDANCEDATE.between(attendanceResultReport.getStartDate(),attendanceResultReport.getEndDate()));
        queryWrapper.where(ATTENDANCE_RESULT_REPORT.USERID.eq(attendanceResultReport.getUserid()));
        log.error("SQL:{}",queryWrapper.toSQL());
        Page<AttendanceResultReport> attendanceResultReportList=this.attendanceResultReportService.page(pageObject,queryWrapper);
        for(AttendanceResultReport attendance:attendanceResultReportList.getRecords()){
            queryWrapper=QueryWrapper.create();
            queryWrapper.where(DING_ATTENDANCE_RESULT.USER_ID.eq(attendance.getUserid()));
            queryWrapper.where(DING_ATTENDANCE_RESULT.WORK_DATE.eq(attendance.getAttendancedate()));
            List<DingAttendanceResult> dingAttendanceResultList=this.dingAttendanceResultService.list(queryWrapper);
            attendance.setDingAttendanceResultList(dingAttendanceResultList);
        }

        return attendanceResultReportList.getRecords().isEmpty()?ResultTool.fail():ResultTool.success(attendanceResultReportList);
    }

}
